package arithmetic1.search;

/*
    二分查找，非递归
 */
public class BinarySearch2 {
    public static void main(String[] args) {
        int arr[]={0,1,2,3,4,5,6,7,8,9};
        System.out.println(binarySearch(2,arr));
    }

    public static int binarySearch(int key,int arr[]){
        int start=0;
        int end=arr.length-1;
        while(start<=end){
            int mid=(start+end)/2;
            int temp=arr[mid];
            if(key<arr[mid]){
                end=mid-1;
            }else if(key>arr[mid]){
                start=mid+1;
            }else {
                return mid;
            }
        }
        return -1;
    }
}
